using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace buildingLib.Prepare
{
    [Serializable()]
    public class AreaShare : BusinessBase<AreaShare>
    {
        #region Business Methods
        int m_id;
        Guid m_buildingid;
        string m_part = string.Empty;
        string m_samename = string.Empty;
        double m_area;
        double m_inarea;
        double m_outarea;
        string m_selfcarrier = string.Empty;
        string m_othercarrier = string.Empty;

        public int ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }

        public Guid Buildingid
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_buildingid;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_buildingid != value)
                {
                    m_buildingid = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Part
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_part;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_part != value)
                {
                    m_part = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Samename
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_samename;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_samename != value)
                {
                    m_samename = value;
                    PropertyHasChanged();
                }
            }
        }
        public double Area
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_area;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_area != value)
                {
                    m_area = value;
                    PropertyHasChanged();
                }
            }
        }
        public double Inarea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_inarea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_inarea != value)
                {
                    m_inarea = value;
                    PropertyHasChanged();
                }
            }
        }
        public double Outarea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_outarea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_outarea != value)
                {
                    m_outarea = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Selfcarrier
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_selfcarrier;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_selfcarrier != value)
                {
                    m_selfcarrier = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Othercarrier
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_othercarrier;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_othercarrier != value)
                {
                    m_othercarrier = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return m_id;
        }
        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        internal static AreaShare NewAreaShare()
        {
            // TODO: change to use new keyword if not loading defaults
            return new AreaShare();
        }

        internal static AreaShare GetAreaShare(SafeDataReader dr)
        {
            return new AreaShare(dr);
        }

        private AreaShare()
        {
            MarkAsChild();
        }

        private AreaShare(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            m_id = dr.GetInt32("id");

            
            m_part = dr.GetString("part");
            m_samename = dr.GetString("samename");
            m_area = dr.GetDouble("area");
            m_inarea = dr.GetDouble("inarea");
            m_outarea = dr.GetDouble("outarea");
            m_selfcarrier = dr.GetString("selfcarrier");
            m_othercarrier = dr.GetString("othercarrier");
            MarkOld();
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GInsertAreaShare");
            db.AddOutParameter(dbcom, "id", DbType.Int32, m_id);
            DoInsertUpdate(dbcom);
            m_id = (Int32)db.GetParameterValue(dbcom, "id");
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GUpdateAreaShare");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);

            DoInsertUpdate(dbcom);
            MarkOld();
        }

        private void DoInsertUpdate(DbCommand dbcom)
        {
            Database db = SQLDatabase.GetDatabase();
            db.AddInParameter(dbcom, "buildingid", DbType.Object, m_buildingid);
            db.AddInParameter(dbcom, "part", DbType.String, m_part);
            db.AddInParameter(dbcom, "samename", DbType.String, m_samename);
            db.AddInParameter(dbcom, "area", DbType.Single, m_area);
            db.AddInParameter(dbcom, "inarea", DbType.Single, m_inarea);
            db.AddInParameter(dbcom, "outarea", DbType.Single, m_outarea);
            db.AddInParameter(dbcom, "selfcarrier", DbType.String, m_selfcarrier);
            db.AddInParameter(dbcom, "othercarrier", DbType.String, m_othercarrier);

            db.ExecuteNonQuery(dbcom);
        }

        internal void DeleteSelf()
        {
            // if we're not dirty then don't update the database
            if (!this.IsDirty) return;

            // if we're new then don't update the database
            if (this.IsNew) return;

            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GDeleteAreaShare");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);

            db.ExecuteNonQuery(dbcom);

            MarkNew();
        }

        #endregion
    }//end of class	
}//end of namespace 

